package com.example.pojo.response;

import com.example.constant.AppHttpCodeEnum;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 6682215287252208284L;

    private int code;
    private String msg;
    private T data;

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static Result ok() {
        return new Result(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getMsg());
    }

    public static <T> Result<T> ok(T data) {
        Result<T> r = new Result<>(AppHttpCodeEnum.SUCCESS.getCode(), AppHttpCodeEnum.SUCCESS.getMsg());
        r.setData(data);
        return r;
    }

    public static Result error() {
        return new Result(AppHttpCodeEnum.INTERNAL_SERVER_ERROR.getCode(), AppHttpCodeEnum.INTERNAL_SERVER_ERROR.getMsg());
    }

    public static Result error(AppHttpCodeEnum appHttpCodeEnum) {
        return new Result(appHttpCodeEnum.getCode(), appHttpCodeEnum.getMsg());
    }

    public static Result error(int code, String msg) {
        return new Result(code, msg);
    }
}